package project.teg.views;

import project.teg.R;
import project.teg.common.types.DeviceType;
import project.teg.common.types.GestureShapeType;
import project.teg.common.types.GestureType;
import project.teg.common.types.HandlingType;
import project.teg.configuration.Gestures;
import project.teg.configuration.Global;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.util.Log;

public class OptionsGestures extends PreferenceActivity implements
		OnPreferenceClickListener, OnPreferenceChangeListener {

	public final static String gridRows = "stg_gridRows";
	public final static String gridColumns = "stg_gridColumns";
	public final static String numberOfCheckpoints = "stg_numberOfCheckpoints";
	public final static String radius = "stg_radius";
	public final static String gestureType = "stg_gestureType";
	public final static String gestureShapeType = "stg_gestureShapeType";
	public final static String pathColor = "stg_pathColor";
	public final static String beginCheckPointColor = "stg_beginCheckPointColor";
	public final static String endCheckpointColor = "stg_endCheckpointColor";
	public final static String internalCheckpointColor = "stg_internalCheckpointColor";
	public final static String backgroundColor = "stg_backgroundColor";
	public final static String interactiveObjectColor = "stg_interactiveObjectColor";
	
	public final static String isBackgroundSolidColor = "stg_isBackgroundSolidColor";
	public final static String isPathVisible = "stg_isPathVisible";
	public final static String isCheckpointIndexVisible = "stg_isCheckpointIndexVisible";
	public final static String minimalDistanceToCheckPoint = "stg_minimalDistanceToCheckPoint";
	

	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.optionsgestures);
		setTitle("Gesture options");
		Gestures.GetAllValues(this);

		EditTextPreference edp = (EditTextPreference) findPreference(gridRows);
		edp.setSummary(String.valueOf(Gestures.gridRows));
		edp.setOnPreferenceChangeListener(this);

		edp = (EditTextPreference) findPreference(gridColumns);
		edp.setSummary(String.valueOf(Gestures.gridColumns));
		edp.setOnPreferenceChangeListener(this);

		edp = (EditTextPreference) findPreference(numberOfCheckpoints);
		edp.setSummary(String.valueOf(Gestures.numberOfCheckpoints));
		edp.setOnPreferenceChangeListener(this);

		edp = (EditTextPreference) findPreference(radius);
		edp.setSummary(String.valueOf(Gestures.radius));
		edp.setOnPreferenceChangeListener(this);
		
		edp = (EditTextPreference) findPreference(minimalDistanceToCheckPoint);
		edp.setSummary(String.valueOf(Gestures.minimalDistanceToCheckPoint));
		edp.setOnPreferenceChangeListener(this);		

		ListPreference lp = (ListPreference) findPreference(gestureType);
		lp.setValue(String.valueOf(Gestures.gestureType.ordinal()));
		lp.setSummary(lp.getEntry());
		lp.setOnPreferenceChangeListener(this);

		lp = (ListPreference) findPreference(gestureShapeType);
		lp.setValue(String.valueOf(Gestures.gestureShapeType.ordinal()));
		lp.setSummary(lp.getEntry());
		lp.setOnPreferenceChangeListener(this);

		lp.setEnabled(Gestures.gestureType == GestureType.Shape);

		IconPreference ips = (IconPreference) findPreference(pathColor);
		ips.circleColor = Gestures.pathColor;
		Log.e("On create", "postavio boju" + Gestures.pathColor);
		ips.setOnPreferenceClickListener(this);

		ips = (IconPreference) findPreference(beginCheckPointColor);
		ips.circleColor = Gestures.beginCheckPointColor;
		ips.setOnPreferenceClickListener(this);

		ips = (IconPreference) findPreference(endCheckpointColor);
		ips.circleColor = Gestures.endCheckpointColor;
		ips.setOnPreferenceClickListener(this);

		ips = (IconPreference) findPreference(internalCheckpointColor);
		ips.circleColor = Gestures.internalCheckpointColor;
		ips.setOnPreferenceClickListener(this);

		ips = (IconPreference) findPreference(backgroundColor);
		ips.circleColor = Gestures.backgroundColor;
		ips.setOnPreferenceClickListener(this);
		
		ips = (IconPreference) findPreference(interactiveObjectColor);
		ips.circleColor = Gestures.interactiveObjectColor;
		ips.setOnPreferenceClickListener(this);
		

		CheckBoxPreference cbp = (CheckBoxPreference) findPreference(isBackgroundSolidColor);
		cbp.setChecked(Gestures.isBackgroundSolidColor);
		cbp.setOnPreferenceClickListener(this);

		cbp = (CheckBoxPreference) findPreference(isCheckpointIndexVisible);
		cbp.setChecked(Gestures.isCheckpointIndexVisible);
		cbp.setOnPreferenceClickListener(this);

		cbp = (CheckBoxPreference) findPreference(isPathVisible);
		cbp.setChecked(Gestures.isPathVisible);
		cbp.setOnPreferenceClickListener(this);
	}

	@Override
	public boolean onPreferenceClick(Preference arg0) {

		if (arg0 instanceof IconPreference) {
			IconPreference ip = (IconPreference) arg0;
			if (ip.getKey().equals(pathColor)) {
				ip.preferenceName = "pathColor";
				ColorPickerDialog d = new ColorPickerDialog(this, ip,
						Gestures.beginCheckPointColor);
				d.show();
			} else if (ip.getKey().equals(beginCheckPointColor)) {
				ip.preferenceName = "beginCheckPointColor";
				ColorPickerDialog d = new ColorPickerDialog(this, ip,
						Gestures.beginCheckPointColor);
				d.show();
			} else if (ip.getKey().equals(endCheckpointColor)) {
				ip.preferenceName = "endCheckpointColor";
				ColorPickerDialog d = new ColorPickerDialog(this, ip,
						Gestures.endCheckpointColor);
				d.show();
			} else if (ip.getKey().equals(internalCheckpointColor)) {
				ip.preferenceName = "internalCheckpointColor";
				ColorPickerDialog d = new ColorPickerDialog(this, ip,
						Gestures.internalCheckpointColor);
				d.show();
			} else if (ip.getKey().equals(backgroundColor)) {
				ip.preferenceName = "backgroundColor";
				ColorPickerDialog d = new ColorPickerDialog(this, ip,
						Gestures.backgroundColor);
				d.show();
			}
			 else if (ip.getKey().equals(interactiveObjectColor)) {
					ip.preferenceName = "interactiveObjectColor";
					ColorPickerDialog d = new ColorPickerDialog(this, ip,
							Gestures.interactiveObjectColor);
					d.show();
				}			
		} else if (arg0 instanceof CheckBoxPreference) {
			CheckBoxPreference cbp = (CheckBoxPreference) arg0;
			if (cbp.getKey().equals(isBackgroundSolidColor)) {
				Gestures.isBackgroundSolidColor = cbp.isChecked();
			} else if (cbp.getKey().equals(isCheckpointIndexVisible)) {
				Gestures.isCheckpointIndexVisible = cbp.isChecked();
			} else if (cbp.getKey().equals(isPathVisible)) {
				Gestures.isPathVisible = cbp.isChecked();
			}

			// Toast.makeText(this, "check", Toast.LENGTH_SHORT).show();
		}
		Gestures.SeveAllValues(this);
		return false;
	}

	@Override
	public boolean onPreferenceChange(Preference arg0, Object arg1) {
		if (arg0 instanceof EditTextPreference) {
			EditTextPreference etp = (EditTextPreference) arg0;
			if (etp.getKey().equals(gridRows)) {
				Gestures.gridRows = Integer.parseInt(etp.getEditText()
						.getText().toString());
			} else if (etp.getKey().equals(gridColumns)) {
				Gestures.gridColumns = Integer.parseInt(etp.getEditText()
						.getText().toString());
			} else if (etp.getKey().equals(numberOfCheckpoints)) {
				Gestures.numberOfCheckpoints = Integer.parseInt(etp
						.getEditText().getText().toString());
			} else if (etp.getKey().equals(radius)) {
				Gestures.radius = Integer.parseInt(etp.getEditText().getText()
						.toString());
			}
			else if (etp.getKey().equals(minimalDistanceToCheckPoint)) {
				Gestures.minimalDistanceToCheckPoint = Integer.parseInt(etp.getEditText().getText()
						.toString());				
			}
			etp.setSummary(etp.getEditText().getText().toString());
		} else if (arg0 instanceof ListPreference) {
			ListPreference lp = (ListPreference) arg0;
			if (lp.getKey().equals(gestureType)) {
				switch (Integer.parseInt(String.valueOf(arg1))) {
				case 0:
					Gestures.gestureType = GestureType.Path;
					break;
				case 1:
					Gestures.gestureType = GestureType.Shape;
					break;
				default:
					break;
				}
				ListPreference lpDetail = (ListPreference)findPreference(gestureShapeType);
				lpDetail.setEnabled(Gestures.gestureType == GestureType.Shape);
			} else {
				switch (Integer.parseInt(String.valueOf(arg1))) {
				case 0:
					Gestures.gestureShapeType = GestureShapeType.Triangle;
					break;
				case 1:
					Gestures.gestureShapeType = GestureShapeType.Circle;
					break;
				case 2:
					Gestures.gestureShapeType = GestureShapeType.Square;
					break;
				case 3:
					Gestures.gestureShapeType = GestureShapeType.Custom;
					break;
				default:
					break;
				}
			}
			lp.setValue(String.valueOf(arg1));
			lp.setSummary(lp.getEntry());

		}
		Gestures.SeveAllValues(this);
		return false;
	}
}
